<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form v-model="listQuery" inline>
        <el-form-item>
          <el-input
            size="small"
            v-model="listQuery.name"
            style="width: 200px"
            class="filter-item"
            placeholder="指标组"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            v-model="listQuery.name"
            style="width: 200px"
            class="filter-item"
            placeholder="时间类型"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="value1"
            size="small"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            class="filter-item"
            style="margin-left: 10px"
            type="success"
            icon="el-icon-plus"
            @click="showCreate"
          >
            添加指标
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-table
        key="tableKey"
        :data="list"
        v-loading="listLoading"
        element-loading-text="加载中"
        border
        fit
        stripe
        highlight-current-row
        style="width: 100%"
        :header-cell-style="{
          background: '#E3ECFB',
          color: '#303133',
          fontWeight: '500'
        }"
      >
        <el-table-column
          align="center"
          label="系统编号"
          sortable
          prop="id"
          width="120"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="指标名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column align="center" label="图片" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-image
              style="width: 60px;"
              :src="scope.row.cover"
              :preview-src-list="scope.row.imglist"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="排名指标">
          <template slot-scope="scope">
            <span>{{ scope.row.url }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="关键指标"
          sortable
          prop="sort"
          class-name="overflow-on"
        >
          <template slot-scope="scope">
            {{ scope.row.sort }}
            <el-input
              size="small"
              v-if="scope.row.show == 1"
              max="50"
              v-model="scope.row.sort"
            />
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="趋势曲线信息"
          prop="status"
          width="120"
        >
          <template slot-scope="scope">
            <el-button>查看</el-button>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="更新时间"
          sortable
          prop="updated_at"
          width="170"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              plain
              v-if="scope.row.show == 1"
              size="small"
              @click="orderHandleDo(scope.row)"
              >保存</el-button
            >
            <el-button plain v-else size="small" @click="orderHandle(scope.row)"
              >排序</el-button
            >
            <el-button
              plain
              size="small"
              type="primary"
              @click="handleEdit(scope.row, 'edit')"
              >编辑</el-button
            >
            <el-button
              plain
              v-if="scope.row.status == 1"
              size="small"
              type="warning"
              @click="changeStatus(scope.row, 0)"
              >去停用</el-button
            >
            <el-button
              plain
              v-else
              size="small"
              type="success"
              @click="changeStatus(scope.row, 1)"
              >去发布</el-button
            >
            <el-button
              plain
              size="small"
              type="danger"
              @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container text-right">
        <el-pagination
          background
          :total="pagination.total"
          :current-page="listQuery.page"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        class="small-space"
        :model="temp"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="指标名称" required>
          <el-input size="small" v-model="temp.name" width="100px"></el-input>
        </el-form-item>
        <el-form-item label="基本信息">
          <el-input size="small" v-model="temp.url"></el-input>
        </el-form-item>
        <el-form-item label="排名指标信息">
          <el-input size="small" v-model="temp.url"></el-input>
        </el-form-item>
        <el-form-item label="关键指标信息">
          <el-input size="small" v-model="temp.url"></el-input>
        </el-form-item>
        <!-- <el-form-item label="图片" required>
          <el-upload
            class="avatar-uploader"
            :action="uploadPath"
            :headers="header"
            :show-file-list="false"
            :on-success="handleImageSuccess"
          >
            <img v-if="temp.cover" :src="temp.cover" style="width: 200px" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item> -->

        <el-form-item label="排序" prop="sort">
          <el-input-number size="small" v-model="temp.sort" />
          <span style="color:#fff;"> 说明：页面按照由小到大展示</span>
        </el-form-item>
        <!-- <el-alert
          title="* 温馨提示：图片尽量压缩后上传，尽量不超过1M"
          type="warning"
        ></el-alert> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="create"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { List, Save, Del, Status, Sort } from "@/api/cooperate";
import { getToken } from "@/utils/auth";
import { uploadPath } from "@/utils/common";

export default {
  data() {
    return {
      list: null,
      joinArr: null,
      changeIndex: -1,
      pagination: {},
      dialogFormVisible: false,
      temp: {
        name: "",
        cover: "",
        sort: 0,
        status: 0
      },
      listQuery: {
        page: 1,
        limit: 10,
        name: ""
      },

      uploadPath: uploadPath,
      dialogStatus: null,
      dialogTitle: "",
      header: {
        Authorization: getToken()
      }
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.listLoading = true;
      List(this.listQuery).then(response => {
        var data = response.data;
        this.list = data.data;
        //图片相对路径处理
        //this.list.forEach(item=>{
        //  item.image_path=process.env.VUE_APP_BASE_PIC +  item.image_path

        //})

        this.pagination = data;

        //图片放大处理，声明一个数组，遍历一遍
        this.list.forEach(e => {
          e.imglist = [];
          e.imglist.push(e.cover);
        });
      });

      this.listLoading = false;
    },

    handleFilter() {
      //console.log(this.listQuery)
      this.listQuery.page = 1;
      this.getList();
    },

    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },

    changeStatus(row, status) {
      Status({ id: row.id, status: status }).then(response => {
        row.status = status;
        this.$message.success("操作成功！");
        this.getList();
      });
    },

    orderHandle(row) {
      row.show = 1;
    },

    orderHandleDo(row) {
      Sort({ id: row.id, sort: row.sort }).then(response => {
        row.show = 0;
        this.getList();
        this.$message.success("操作成功！");
      });
    },

    showCreate() {
      this.temp = {
        name: "",
        cover: "",
        status: 0,
        sort: 0
      };
      this.dialogTitle = "添加";
      this.dialogStatus = "create";
      (this.dialogTitle = "添加"), (this.dialogFormVisible = true);
    },

    handleEdit(row, status) {
      this.dialogTitle = "编辑";
      this.dialogFormVisible = true;
      this.temp = JSON.parse(JSON.stringify(row));
      this.dialogStatus = status;
    },

    handleDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          Del({ id: row.id }).then(response => {
            if (response.code === 0) {
              this.$message({
                message: response.msg,
                type: "success"
              });
              this.dialogFormVisible = false;
              this.getList();
            } else {
              this.$message({
                message: response.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    handleImageSuccess(res, file) {
      if (res.code === 0) {
        this.temp.cover = res.data.filepath;
        this.$message.success("上传成功！");
      } else {
        this.$message.error(res.msg);
      }
    },

    update() {
      Save(this.temp).then(response => {
        if (response.code === 0) {
          this.$message({
            message: response.msg,
            type: "success"
          });
          this.dialogFormVisible = false;
          //this.fetchSetting()
          this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: "error"
          });
        }
      });
    },
    create() {
      Save(this.temp).then(response => {
        if (response.code === 0) {
          this.$message({
            message: response.msg,
            type: "success"
          });
          this.dialogFormVisible = false;
          //this.fetchSetting()
          this.getList();
        }
      });
    }
  }
};
</script>

<style>
/* .table-container {
  margin: 20px 0;
} */
/* .alert-container {
  margin: 20px 0;
} */
</style>
